%% @author Pius Uzamere <pius@uyiosa.com>
%%   [http://uyiosa.com]

%% @author Scott White <scott@3rdarm.com>
%%   [http://www.3rdarm.com]


%% Right now, all tests are included inline, which means that you need EUnit installed to compile this code.
%% See here for instructions: http://pragdave.pragprog.com/pragdave/2007/04/testfirst_word_.html


-include_lib("eunit/include/eunit.hrl").
-module(big_dogs).
-export([starts_with/2, ends_with/2,at/2,to/2,from/2]).


%% starts_with/2
%% Takes two strings as arguments
%% and returns true iff String begins with Pattern
starts_with(String,Pattern) ->
      lists:prefix(Pattern, String).

%% Test for starts_with/2.
starts_with_test_() -> [
?_assert(starts_with("","") == true),
?_assert(starts_with("cat", "c") == true),
?_assert(starts_with("cat", "ca") == true),
?_assert(starts_with("cat", "cat") == true),
?_assert(starts_with("cat", "a") == false)
].

%% ends_with/2
%% Takes two strings as arguments
%% and returns true iff String ends with Pattern
ends_with(String,Pattern) ->
      lists:suffix(Pattern, String).

%% Test for ends_with/2.
ends_with_test_() -> [
?_assert(ends_with("","") == true),
?_assert(ends_with("cat", "t") == true),
?_assert(ends_with("cat", "at") == true),
?_assert(ends_with("cat", "cat") == true),
?_assert(ends_with("cat", "a") == false)
].


%% at/2
%% Takes a string and postion as arguments
%% and returns the character at the position
%% returns empty string if not found
at(String,Pos) ->
      try string:sub_string(String,Pos,Pos) 
      catch
        _:_ -> ""
      end.
%% Test for at/2.
at_test_() -> 
X="My test string",
[
?_assert(at(X, 1) == "M"),
?_assert(at(X, 100) == ""),
?_assert(at(X, 3) == " ")
].

%% to/2
%% Takes a string and postion as arguments
%% and returns the string from that position 
%% Equalivant to string:left w/o the char parameter
to(String,Pos) ->
      try string:sub_string(String,1,Pos)
      catch
        _:_ -> ""
      end.
%% Test for at/2.
to_test_() -> 
X="My test string",
[
?_assert(to(X, 1) == "M"),
?_assert(to(X, 100) == X),
?_assert(to(X, -3) == "")
].

%% from/2
%% Takes a string and postion as arguments
%% and returns the string from that position.
%% Equalivant to string:substr with length always
%% equal to size of the string.
from(String,Pos) ->
			Size = string:len(String),
			if 
				Pos > Size -> "";
				Pos < 0 -> String;
				true ->
      		string:substr(String,Pos,Size)
			end.
%% Test for at/2.
from_test_() -> 
X="My test string",
[
?_assert(from(X, 1) == X),
?_assert(from(X, 2) == "y test string"),
?_assert(from(X, 100) == ""),
?_assert(from(X, -3) == X)
].
      